using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;

public partial class GonzalesViewFile : System.Web.UI.Page
{
    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;
    protected SupportingDocument ResultDoc = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();

        if (Request.QueryString["id"] != null && Request.QueryString["id"].Length == 36)
        {
            using (FileManager CurFileManager = new FileManager(this.CurrentPrivateSession))
            {
                this.ResultDoc = CurFileManager.GetFileByID(Request.QueryString["id"]);
            }

            if (this.ResultDoc != null)
            {
                Response.ContentType = this.GetContentType(this.ResultDoc.FileType);
                //Response.AddHeader("content-disposition","attachment;filename=excel.xlsx");
                Response.Expires = 0;
                Response.Buffer = true;
                Response.Clear();
                Response.BinaryWrite(this.ResultDoc.BufferContent);
                Response.End();
            }
        }
        else
        {
            Response.End();
        }
    }

    protected string GetContentType(string FileType)
    {
        string ResultContentType = "";

        switch (FileType.ToLower())
        {
            case "gif":
                ResultContentType = "image/gif";
                break;

            case "jpg":
                ResultContentType = "image/jpeg";
                break;

            case "jpe":
                ResultContentType = "image/jpeg";
                break;

            case "jpeg":
                ResultContentType = "image/jpeg";
                break;

            case "png":
                ResultContentType = "image/png";
                break;                

            case "htm":
                ResultContentType = "text/html";
                break;

            case "html":
                ResultContentType = "text/html";
                break;

            case "pdf":
                ResultContentType = "application/pdf";
                break;

            case "doc":
                ResultContentType = "application/msword";
                break;

            case "rtf":
                ResultContentType = "application/msword";
                break;

            case "xls":
                ResultContentType = "application/vnd.ms-excel";
                break;

            case "xlsx":
                ResultContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                break;

            case "docx":
                ResultContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                break;

            case "pptx":
                ResultContentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
                break;

            case "ppt":
                ResultContentType = "application/vnd.ms-powerpoint";
                break;

            case "pps":
                ResultContentType = "application/vnd.ms-powerpoint";
                break;

            case "txt":
                ResultContentType = "text/plain";
                break;

            default:
                ResultContentType = "application/octet-stream";
                break;

        }


        return ResultContentType;
    }

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion
}
